import Vue from "vue";
import VueRouter from "vue-router";
import Layout from '@/layout'
import { isAllow } from "@/untils/roles";


Vue.use(VueRouter);

/**
 * hidden:菜单显隐
 * meta中activeMenu：内层菜单隐藏时，外层菜单仍处于选中状态
 * meta中title必填，用于菜单鉴权
 */
export const permissionsRoute = [
    {
        path: '/user',
        component: Layout,
        redirect: '/user/account',
        meta: { title: '用户管理', icon: 'el-icon-user-solid' },
        children: [
            {
                path: 'account',
                component: () => import('@/views/user/account'),
                name: 'Account',
                meta: { title: '账号管理' }
            },

        ]
    },
    {
        path: '/dataManage',
        component: Layout,
        redirect: '/dataManage/index',
        meta: { title: '数据管理', icon: 'el-icon-s-data' },
        children: [
            {
                path: 'index',
                component: () => import('@/views/dataManage/index'),
                name: 'Index',
                meta: { title: '线缆管理' }
            },

        ]
    },
    {
        path: '/weightManage',
        component: Layout,
        redirect: '/weightManage/weightIn',
        meta: { title: '称重管理', icon: 'el-icon-s-tools' },
        children: [
            {
                path: 'weightIn',
                component: () => import('@/views/weightManage/weightIn'),
                name: 'WeightIn',
                meta: { title: '单位转换' }
            },
            {
                path: 'weightLog',
                component: () => import('@/views/weightManage/weightLog'),
                name: 'WeightLog',
                meta: { title: '称重记录' }
            },

        ]
    },
    {
        path: '/logManage',
        component: Layout,
        redirect: '/logManage/userLog',
        meta: { title: '日志管理', icon: 'el-icon-menu' },
        children: [
            {
                path: 'userLog',
                component: () => import('@/views/logManage/userLog'),
                name: 'UserLog',
                meta: { title: '用户登录管理' }
            },
            {
                path: 'sysLog',
                component: () => import('@/views/logManage/sysLog'),
                name: 'SysLog',
                meta: { title: '系统异常日志' }
            },

        ]
    },
]
export const openRouter = [
    {
        path: '/',
        redirect: '/login',// 当访问根路径时重定向到登录页
        hidden: true,
        meta: { title: 'login' }
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/login'),
        hidden: true,
        meta: { title: 'login' }
    },
    {
        path: '/error',
        component: Layout,
        redirect: '/error/401',
        meta: { title: '错误页面', icon: 'el-icon-s-tools' },
        children: [
            {
                path: '401',
                component: () => import('@/views/error-page/401'),
                name: 'Page401',
                meta: { title: '401' }
            },
        ]
    },
]

export const allRouter = [
    ...openRouter,
    ...permissionsRoute
]

const router = new VueRouter({
    mode: 'hash', // 使用 hash 模式
    routes: allRouter
});
router.beforeEach((to, from, next) => {
    let userInfo = localStorage.getItem('userInfo')
    let menu = JSON.parse(localStorage.getItem('menu'))
    if (to.path !== '/login' && !userInfo) {
        return next('/login'); // 如果需要认证但用户未登录，则重定向到登录页
    }
    if (to.path === '/login' && userInfo && menu && menu.length) {
        return next(menu[0]?.url); // 如果用户已登录并试图访问登录页，则重定向有权限访问的第一页
    }
    let status = isAllow(to.meta.title)
    if (!status) {                           //判断角色逻辑
        return next('/error/401')
    }
    next();
});
export default router;